

$(function () {
    $("#btn_name_update").click(function () {
        $("#btn_name_update").attr("disabled",true);
        var name = $("#name").val();
        var nickname = $("#nickname").val();

        if(valid_name(name,nickname)){
            var params = $("#update_name_form").serialize();
            $.ajax({
                type:"POST",
                url:"/update/user/name",
                data:params,
                success:function (r) {
                    if(r == 'success'){
                        layer.open({
                            icon:1,
                            title:"success",
                            content: "修改成功"
                        });
                    }else{
                        layer.open({
                            icon:2,
                            title:"fail",
                            content: "修改失败"
                        });
                    }
                    $("#btn_name_update").attr("disabled",false);
                }
            });

        }else{
            $("#btn_name_update").attr("disabled",false);
        }

    });


    $("#btn_password_update").click(function () {
        $("#btn_password_update").attr("disabled",true);
        var password = $("#password").val();
        var npsd = $("#npsd").val();
        var rnpsd = $("#rnpsd").val();
        $("#hpassword").val(md5(password));
        $("#hnpsd").val(md5(npsd));

        if(valid_password(password,npsd,rnpsd)){
            var params = $("#update_password_form").serialize();
            $.ajax({
                type:"POST",
                url:"/update/user/password",
                data:params,
                success:function (r) {
                    if(r=='success'){
                        layer.open({
                            icon:1,
                            title:"success",
                            content: "修改成功"
                        });

                        window.location.href='/myLogin';
                    }else{
                        layer.open({
                            icon:2,
                            title:"fail",
                            content: r
                        });
                        $("#btn_password_update").attr("disabled",false);
                    }
                }
            });
        }else{
            $("#btn_password_update").attr("disabled",false);
        }
    });
})

function valid_name(name, nickname) {
    if(isNull(name) || name.trim().length < 1){
        $("#msg_name").css("display","block");
        $("#msg_name").html("请输入登陆名称！");
        return false;
    }
    if(isNull(nickname) || nickname.trim().length < 1){
        $("#msg_name").css("display","block");
        $("#msg_name").html("请输入昵称！");
        return false;
    }

    if(!reg_valid_name(name)){
        $("#msg_name").css("display","block");
        $("#msg_name").html("登陆名称格式为4~20字符！");
        return false;
    }
    if(!reg_valid_name(nickname)){
        $("#msg_name").css("display","block");
        $("#msg_name").html("昵称格式为4~20字符！");
        return false;
    }
    return true;
}

function valid_password(password,npsd,rnpsd) {

    // 1 格式的判断
    if(isNull(password) || password.trim().length < 1){
        $("#msg").css("display","block");
        $("#msg").html("请输入原密码！");
        return false;
    }

    if(isNull(npsd)||npsd.trim().length<1){
        $("#msg").css("display","block");
        $("#msg").html("请输入新密码！");
        return false;
    }

    if(isNull(rnpsd)||rnpsd.trim().length<1){
        $("#msg").css("display","block");
        $("#msg").html("请输入确认密码！");
        return false;
    }

    if(!reg_valid_password(npsd)){
        $("#msg").css("display","block");
        $("#msg").html("新密码格式6~20个字符！");
        return false;
    }

    // 2 新密码和确认密码匹配
    if(npsd != rnpsd){
        $("#msg").css("display","block");
        $("#msg").html("新密码和确认密码不一致！");
        return false;
    }

    return true;
}






